package com.leo.neo4j.config;

import com.leo.neo4j.Neo4jApplication;
import com.leo.neo4j.mysql.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 文件名：ServletInitializer.java
 *
 * @create 2019-11-27 14:18
 * <p>
 * <p> 定时任务调用被spring管理的bean
 * <p>
 * 北京中油瑞飞信息技术有限责任公司(http://www.richfit.com)
 * Copyright  2017 Richfit Information Technology Co., LTD. All Right Reserved.
 */
@Component
public class ServletInitializer extends SpringBootServletInitializer implements ApplicationListener<ApplicationEvent> {
    private Logger logger = LoggerFactory.getLogger(">>定时任务启动器<<");
    @Resource
    private UserMapper userMapper;



    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Neo4jApplication.class);
    }
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
//        System.out.println(snowflake.nextId());
//        System.out.println(snowflake.nextIdStr());
        if (event instanceof ContextClosedEvent || event instanceof ContextStoppedEvent) {
//			ThirdSystem.out.println(event.getClass().getSimpleName() + " 事件已发生！");
        } else if (event instanceof ContextRefreshedEvent) {
            ContextRefreshedEvent evt = (ContextRefreshedEvent) event;
            if(evt.getApplicationContext().getParent() == null){
                //存一下上下文，反射执行方法要用
                //SpringContextUtil.setApplicationContext(evt.getApplicationContext());
                //SpringUtil.setApplicationContext(evt.getApplicationContext());
                //需要执行的逻辑代码，当spring容器初始化完成后就会执行该方法。
                //初始化定时任务,读取定时任务标志参数，1表示定时任务系统，0业务系统，业务系统不加载定时任务
//                Object qartz = env.getProperty("app.initialLoadQuartz");
//                if(!AppConst.ENV_PROD_KEY.equals(appConfigure.getProfile())) {
//                    logger.error("------------->\nhttp://" + HttpRequestUtil.getHostAddress( ) + ":" + serverPort + "/doc.html");
//                }
            }
        }

        logger.info("============================= "+(userMapper.selectList(null)==null));
    }

}
